package com.dianshang.controller.goods;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dianshang.entity.PageResult;
import com.dianshang.entity.Result;
import com.dianshang.pojo.goods.Album;
import com.dianshang.service.goods.AlbumService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/album")
public class AlbumController {

    @Reference
    private AlbumService albumService;

    /**
     * 查询所有
     */
    @GetMapping("/findAll")
    public List<Album> findAll(){
        return albumService.findAll();
    }

    /**
     * 查询所有+分页
     */
    @GetMapping("/findPage")
    public PageResult<Album> findPage(Integer page, Integer size){
        return albumService.findPage(page, size);
    }

    /**
     * 查询所有+模糊
     */
    @PostMapping("/findList")
    public List<Album> findList(@RequestBody Map<String,Object> searchMap){
        return albumService.findList(searchMap);
    }

    /**
     * 查询所有+分页+模糊
     */
    @PostMapping("/findPage")
    public PageResult<Album> findPage(@RequestBody Map<String,Object> searchMap,Integer page, Integer size){
        return  albumService.findPage(searchMap,page,size);
    }

    /**
     * 回显
     */
    @GetMapping("/findById")
    public Album findById(Integer id){
        return albumService.findById(id);
    }

    /**
     * 添加
     */
    @PostMapping("/add")
    public Result add(@RequestBody Album album){
        albumService.add(album);
        return new Result();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Result update(@RequestBody Album album){
        albumService.update(album);
        return new Result();
    }

    /**
     * 删除
     */
    @GetMapping("/delete")
    public Result delete(Integer id){
        albumService.delete(id);
        return new Result();
    }

    /**
     * 根据id查询相册的图片列表，字符串转换为数组
     */
    @GetMapping("findImg")
    public Object findImg(Integer id){
        Album album = albumService.findById(id);
        String imageItems = album.getImageItems();
        return JSONArray.parse(imageItems);
    }

    /**
     * 添加图片列表，并保存到相册
     */
    @PostMapping("addImg") // uid和url俩个字段
    public Result addImg(@RequestBody Map<String,Object> searchMap,Integer id){
        Album album = albumService.findById(id);
        String imageItems = album.getImageItems();
        JSONArray jsonArray = JSONArray.parseArray(imageItems);
        searchMap.put("uid", UUID.randomUUID());
        jsonArray.add(searchMap);
        album.setImageItems(jsonArray.toJSONString());
        albumService.update(album);
        return new Result();
    }

    /**
     * 删除图片列表
     */
    @GetMapping("deleteImg")
    public Result deleteImg(Integer id,String uid){
        Album album = albumService.findById(id);
        String imageItems = album.getImageItems();
        JSONArray jsonArray = JSONArray.parseArray(imageItems);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject json = jsonArray.getJSONObject(i);
            if (json.get("uid").equals(uid)){
                jsonArray.remove(json);
            }
        }
        album.setImageItems(jsonArray.toJSONString());
        albumService.update(album);
        return new Result();
    }

}